// pages/mys/guo/set/taskOne/index.js
const app = getApp(); // 获取应用实例
var Utils = require('../../../../../utils/crypt.js'); //加密解密

Page({

  /**
   * 页面的初始数据
   */
  data: {
    groupId: 0, groupName: '', treeOks: [], treeNos: [], 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var that = this;
    var id = options.id;
    var name = options.name;
    that.setData({groupId:id, groupName:name});
    that.getIndex();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //返回
  toBack: function () {
    console.log('返回');
    wx.showToast({
      title: '即将返回。。', icon: 'none'
    })
    wx.redirectTo({
      url: '/pages/mys/guo/set/index'
    })
  },
  //获取果树列表
  getIndex: function () {
    var that = this;
    var uwx = app.globalData.usertest;
    var userType = app.globalData.userType;
    var gid = that.data.groupId;
    var formData = {
      wx: uwx, userType: userType, group: gid,
    };
    //请求数据
    wx.request({
      url: app.globalData.domain+'/my/guo/groupno/index',
      method: "POST",
      header: {'content-type': 'application/x-www-form-urlencoded'},
      data: formData,
      success: function (res) {
        // wx.hideLoading();
        console.log(res);
        if (res.data.code!=0) {
          wx.showToast({
            title: res.data.msg, icon: 'none',
          }); return
        } 
        console.log(res.data.data.treeNos);
        console.log(res.data.data.treeOks);
        that.setData({
          treeNos: res.data.data.treeNos, 
          treeOks: res.data.data.treeOks,
        });
      }
    });
  },
  //去选中
  toOk: function (e) {
    console.log(e);
    var that = this;
    var uwx = app.globalData.usertest;
    var gid = that.data.groupId;
    var tid = e.currentTarget.dataset.id;
    var formData = {
      wx: uwx, gid: gid, tid: tid,
    };
    //请求数据
    wx.request({
      url: app.globalData.domain+'/my/guo/tree/add',
      method: "POST",
      header: {'content-type': 'application/x-www-form-urlencoded'},
      data: formData,
      success: function (res) {
        wx.hideLoading();
        if (res.data.code!=0) {
          wx.showToast({
            title: res.data.msg, icon: 'none',
          }); return
        } 
        wx.showToast({
          title: '保存成功..', icon: 'none',
        }); 
        setTimeout(function(){
          that.getIndex();
          // wx.redirectTo({
          //   url: '/pages/mys/guo/set/taskOne/index'
          // });
        },2000);
      }
    });
  },
  //去取消
  toNo: function (e) {
    console.log(e);
    var that = this;
    var uwx = app.globalData.usertest;
    var id = e.currentTarget.dataset.id;
    var formData = {
      wx: uwx, id: id,
    };
    //请求数据
    wx.request({
      url: app.globalData.domain+'/my/guo/tree/del',
      method: "POST",
      header: {'content-type': 'application/x-www-form-urlencoded'},
      data: formData,
      success: function (res) {
        wx.hideLoading();
        if (res.data.code!=0) {
          wx.showToast({
            title: res.data.msg, icon: 'none',
          }); return
        } 
        wx.showToast({
          title: '删除成功..', icon: 'none',
        }); 
        setTimeout(function(){
          that.getIndex();
          // wx.redirectTo({
          //   url: '/pages/mys/guo/set/taskOne/index'
          // });
        },2000);
      }
    });
  },
})